package org.dromara.web.service.business;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import org.dromara.common.core.exception.ServiceException;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.dao.business.domain.UserApiRecord;
import org.dromara.dao.business.domain.UserApiRecordQuery;
import org.dromara.dao.business.mapper.UserApiRecordMapper;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;

@Service
public class UserApiRecordService {

    @Autowired
    UserApiRecordMapper userApiRecordMapper;


    public TableDataInfo<UserApiRecord> queryPageList(UserApiRecordQuery query, PageQuery pageQuery) {
        return new TableDataInfo(
                userApiRecordMapper.paginationSelectList(query, pageQuery),
                userApiRecordMapper.paginationSelectCount(query)
        );
    }


    public UserApiRecord queryById(Long id) {
        return userApiRecordMapper.selectById(id);
    }


    @Transactional
    public UserApiRecord insert(UserApiRecord entity) {
        userApiRecordMapper.insert(entity);
        return entity;
    }

    @Transactional
    public void updateById(UserApiRecord update) {

        UserApiRecord entity = userApiRecordMapper.selectById(update.getId());
        if (entity == null) {
            throw new ServiceException();
        }
        entity.setUserId(update.getUserId());
        entity.setUrl(update.getUrl());
        entity.setIp(update.getIp());
        entity.setPort(update.getPort());
        entity.setSessionId(update.getSessionId());
        entity.setUpdateTime(LocalDateTime.now());
        userApiRecordMapper.updateById(entity);
    }


    @Transactional
    public void deleteById(Long id) {
        userApiRecordMapper.deleteById(id);
    }

    @Transactional
    public void deleteByIds(Long[] ids) {
        userApiRecordMapper.deleteByIds(Arrays.asList(ids));
    }
}
